package br.com.automed.service;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.automed.entity.Consulta;
import br.com.automed.entity.Paciente;
import br.com.automed.hibernate.HibernateUtil;

public class ConsultaBusiness {
	
	private static ConsultaBusiness instancia;
	
	private ConsultaBusiness() {
	}
	
	public static ConsultaBusiness getInstancia(){
		if(instancia == null){
			instancia = new ConsultaBusiness();
		}
		return instancia;
	}
	
	public boolean marcarConsulta(Consulta consulta, Paciente paciente){
		
		Session session	= null;
		Transaction tx	= null;
		
        try {
        	session = HibernateUtil.getSessionFactory().getCurrentSession();
			if ( !session.isOpen() ) {
				session = HibernateUtil.getSessionFactory().openSession();
			}
			session.setFlushMode(FlushMode.MANUAL);
        	tx = session.beginTransaction();
        	//Query query = session.createQuery("select o from Consulta o where o.idConsulta = :param");
        	//query.setLockMode("o", LockMode.UPGRADE_NOWAIT);
        	//query.setParameter("param", consulta.getIdConsulta());
        	Consulta c = (Consulta) session.get(Consulta.class, consulta.getIdConsulta(), LockOptions.UPGRADE);
        	//session.lock(c, LockMode.UPGRADE_NOWAIT);
        	if(c.getPaciente() != null){
        		return false;
        	}
        	c.setPaciente(paciente);
        	c.setConvenio(paciente.getConvenio());
        	c.setFlagAtendido("N");
			session.update(c);
			session.flush();
			tx.commit();
			return true;
        } catch (HibernateException e) {
			if ( tx != null) {
				tx.rollback();
			}
			return false;
		} catch (Exception e) {
			if ( tx != null) {
				tx.rollback();
			}
			return false;
		} finally {
			tx 		= null;
			if ( session != null && session.isOpen() ) {
				session.close();
				session	= null;
			}
		}
		
	}
	
	public boolean cancelarConsulta(Consulta consulta){
		
		Session session	= null;
		Transaction tx	= null;
		
        try {
        	session = HibernateUtil.getSessionFactory().getCurrentSession();
			if ( !session.isOpen() ) {
				session = HibernateUtil.getSessionFactory().openSession();
			}
			session.setFlushMode(FlushMode.MANUAL);
        	tx = session.beginTransaction();
        	Consulta c = (Consulta) session.get(Consulta.class, consulta.getIdConsulta(), LockOptions.UPGRADE);
        	
        	if(c == null){
        		return false;
        	}
        	c.setPaciente(null);
        	c.setConvenio(null);
        	c.setFlagAtendido(null);
			session.update(c);
			session.flush();
			tx.commit();
			return true;
        } catch (HibernateException e) {
			if ( tx != null) {
				tx.rollback();
			}
			return false;
		} catch (Exception e) {
			if ( tx != null) {
				tx.rollback();
			}
			return false;
		} finally {
			tx 		= null;
			if ( session != null && session.isOpen() ) {
				session.close();
				session	= null;
			}
		}
		
	}

}
